/**
 * 
 */
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Hashtable;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




import Action.Clip;
import Bean.ClipBean;
import Login.CheckLogin;
import Utils.Constant;
import Utils.DbAccess;
import Utils.FileUtils;
import Utils.GenUtils;
import Utils.ImageUtils;
import Utils.Random;

import Utils.ZipResponse;
import Utils.upload.FileUpload;
import Utils.upload.Section;






public class UploadClip extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 4194452284080389577L;
	
	DbAccess db = null;
	
	public void init() {
		try {
			db = new DbAccess(Constant.DB,true);
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.setContentType("text/html");
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");
		if(!CheckLogin.check(req)){
			ZipResponse.write(
				req,
				res,
				"<html><script>window.location='blank.htm';window.parent.onUpload('notlogin','');</script></html>",
				"UTF-8");
			return;
		}
		Connection conn = null;
		PreparedStatement stmt = null;
		String folder = "";
		try{
			conn = db.getConn();
			ServletContext ctx = this.getServletContext();
			String rootPath = ctx.getRealPath("/");
			if (!rootPath.endsWith("/"))
				rootPath += "/";
			folder =rootPath + "WEB-INF/uploads/news/"+Random.randomString(3);
			File f = new File(folder);
			if (!f.exists())
				f.mkdirs();
			FileUpload fileUpload = new FileUpload(f.getAbsolutePath(), 1000*1024*100);
			Hashtable uploads = null;
			uploads = fileUpload.getUpload(req, res);
			ClipBean bean = new ClipBean();
			String clipName = ((Section) uploads.get("clipName")).getValue().trim();
			String clipDesc = ((Section) uploads.get("clipDesc")).getValue().trim();
			String clipPath_ = ((Section) uploads.get("clipPath")).getValue().trim();
			String clipThumbnail_ = ((Section) uploads.get("clipThumbnail")).getValue().trim();
			String clipDate = ((Section) uploads.get("clipDate")).getValue().trim();
			String catid = ((Section) uploads.get("catid")).getValue().trim();
			String action = ((Section) uploads.get("action")).getValue().trim();
			String clipId = Random.randomNumber(5);
			
			if(!action.equals("add"))
				clipId = ((Section) uploads.get("clipId")).getValue().trim();
			
			String folder_clip = GenUtils.genPathByClipId(clipId);
			String clipPath = ImageUtils.uploadClip(uploads, folder_clip);
			String folder_img = GenUtils.genPathByPictureId(clipId);
			String thumbnail = ImageUtils.uploadImageClip(uploads, folder_img);
			
			bean.setClipDescription(clipDesc);
			bean.setClipId(clipId);
			bean.setClipPath(clipPath_);
			bean.setClipName(clipName);
			bean.setClipThumbnail(clipThumbnail_);
			bean.setClipDate(clipDate);
			bean.setClipCat(catid);
			
			if(!clipPath.equals("")){
				bean.setClipPath(clipPath);
			}
			if(!thumbnail.equals("")){
				bean.setClipThumbnail(thumbnail);
			}
			if(action.equals("add")){
				if(new Clip().insertClip(bean, conn)){
					ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('true','');</script></html>",
					"UTF-8");
					return;
				}
			}else{
				if(new Clip().updateClip(bean, conn)){
					ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('true','');</script></html>",
					"UTF-8");
					return;
				}
			}
			ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('false','error');</script></html>",
				"UTF-8");
		}catch (Exception e) {
			e.printStackTrace();
			ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('false','');</script></html>",
			"UTF-8");
		}finally{
			if(stmt!=null){try{stmt.close();}catch (Exception e) {}}
			if(conn!=null){try{conn.close();}catch (Exception e) {}}
			FileUtils.deleteDir(new File(folder));
		}
		ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('true','');</script></html>",
				"UTF-8");
	}
	
}
